package net.jiastudy.web.rest.controller.targettask;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.jiastudy.biz.targettask.entity.TaskYear;
import net.jiastudy.biz.targettask.service.TargetTaskService;
import net.jiastudy.core.base.BaseController;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/targettask/years")
@Api(value = "TargetTaskController  目标任务")
public class TargetTaskYearController extends BaseController {
    @Resource
    private TargetTaskService targetTaskService;

    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "新增目标年度", notes = "")
    public TaskYear create(@Validated @RequestBody TaskYear taskYear) {
        return targetTaskService.createTaskYear(taskYear);
    }

    @RequestMapping(path = "/{id}", method = RequestMethod.PUT)
    @ApiOperation(value = "编辑目标年度", notes = "")
    public TaskYear edit(@PathVariable Long id, @Validated @RequestBody TaskYear taskYear) {
        return targetTaskService.updateTaskYear(taskYear);
    }

    @RequestMapping(path = "/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取目标年度", notes = "")
    public TaskYear edit(@PathVariable Long id) {
        return targetTaskService.getTaskYear(id);
    }

    @ApiOperation(value = "分页年度", notes = "")
    @RequestMapping(method = RequestMethod.GET)
    public Page<TaskYear> page(@RequestParam Map<String, String> conditions,
                               @PageableDefault(value = 20, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
        Page<TaskYear> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        Sort sort = pageable.getSort();

        return targetTaskService.searchTaskYears(page, conditions, sort);
    }

    @ApiOperation(value = "搜索栏年度", notes = "")
    @RequestMapping(path = "/all", method = RequestMethod.GET)
    public List<TaskYear> findAll() {
        return targetTaskService.findAllTaskYears();
    }
}
